package com.xy.minio;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class FileQuery {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, InvalidResponseException, BucketPolicyTooLargeException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, RegionConflictException {
        MinioClient client = MinioClient
                .builder()
                .endpoint("http://106.12.149.245:9000")
                .credentials("admin", "admin123")
                .build();
        Iterable<Result<Item>> iter = client.listObjects(ListObjectsArgs.builder().bucket("test").recursive(true).build());
        System.out.println(fileExists(client, "test", "aaa"));
//        iter.forEach( (it) -> {
//            try {
//                System.out.println(it.get().objectName());
//            } catch (ErrorResponseException e) {
//                e.printStackTrace();
//            } catch (InsufficientDataException e) {
//                e.printStackTrace();
//            } catch (InternalException e) {
//                e.printStackTrace();
//            } catch (InvalidBucketNameException e) {
//                e.printStackTrace();
//            } catch (InvalidKeyException e) {
//                e.printStackTrace();
//            } catch (InvalidResponseException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (NoSuchAlgorithmException e) {
//                e.printStackTrace();
//            } catch (ServerException e) {
//                e.printStackTrace();
//            } catch (XmlParserException e) {
//                e.printStackTrace();
//            }
//        })
    }

    static boolean fileExists(MinioClient client, String bucketName, String objName) {
        boolean fileExists = false;
        try(InputStream is = client.getObject(GetObjectArgs.builder().bucket(bucketName).object(objName).build())) {
            fileExists = true;
        } catch (Exception e) {
//            e.printStackTrace();
        } finally {
            return fileExists;
        }
    }
}
